package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJsonData;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.entity.User;
import com.micro.modual.entity.UserAccountJournal;
import com.micro.modual.entity.UserStatisticsInfo;
import com.micro.modual.mapper.UserAccountJournalMapper;
import com.micro.modual.mapper.UserMapper;
import com.micro.modual.mapper.UserStatisticsInfoMapper;
import com.micro.modual.model.dto.member.MemberAccountDTO;
import com.micro.modual.model.dto.member.MemberDetailDTO;
import com.micro.modual.model.dto.member.MemberListDTO;
import com.micro.modual.model.dto.member.MemberStatisticsInfoDTO;
import com.micro.modual.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020/7/23
 */
@Service("memberService")
public class MemberServiceImpl implements MemberService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserStatisticsInfoMapper userStatisticsInfoMapper;

    @Autowired
    private UserAccountJournalMapper userAccountJournalMapper;

    @Override
    public RespJsonPageData<MemberListDTO> findMemberList(Map<String, Object> map) {
        PageQuery pageQuery = new PageQuery(map);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<User> userList = userMapper.findMemberList(map);
        List<MemberListDTO> memberListDTOList = JacksonUtil.getList(JacksonUtil.toJson(userList),MemberListDTO.class);
        return new RespJsonPageData<MemberListDTO>(memberListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }


    @Override
    public RespJsonData<MemberDetailDTO> findMemberDetail(String memberCode) {
        User user = userMapper.findMemberDetail(memberCode);
        MemberDetailDTO memberDetailDTO = JacksonUtil.getObject(JacksonUtil.toJson(user),MemberDetailDTO.class);
        return new RespJsonData<MemberDetailDTO>(memberDetailDTO,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }


    @Override
    public RespJsonData<MemberStatisticsInfoDTO> findMemberStatisticsInfoDetail(String memberCode) {
        UserStatisticsInfo userStatisticsInfo = userStatisticsInfoMapper.findMemberStatisticsInfoDetail(memberCode);
        MemberStatisticsInfoDTO memberStatisticsInfoDTO = JacksonUtil.getObject(JacksonUtil.toJson(userStatisticsInfo),MemberStatisticsInfoDTO.class);
        return new RespJsonData<MemberStatisticsInfoDTO>(memberStatisticsInfoDTO,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJsonPageData<MemberAccountDTO> findMemberAccountList(Map<String, Object> map) {
        PageQuery pageQuery = new PageQuery(map);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<UserAccountJournal> userAccountJournalList =  userAccountJournalMapper.findMemberAccountList(map);
        List<MemberAccountDTO> memberAccountDTOList = JacksonUtil.getList(JacksonUtil.toJson(userAccountJournalList),MemberAccountDTO.class);
        return new RespJsonPageData<MemberAccountDTO>(memberAccountDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }
}
